//
//  PywPayFootView.m
//  PywSdk
//
//  Created by Jade on 16/7/22.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywPayFootView.h"
#import "PywMasonry.h"
#import "MutableDictionary.h"
#import "UIImage+Pyw.h"
#import "PywAppInfo.h"

/**
 *  支付方式按钮
 */
@interface PywPayFootItemView : UIControl
@property (nonatomic,assign) NSInteger payWay;
@property (nonatomic,strong) UIImageView *payBageImgview;//角标
@property (nonatomic,strong) UIImageView *payIconImgView;//支付方式图标
@property (nonatomic,strong) UILabel *payNameLabel;//支付方式标题

@end
@class AIFCreateOrderManager;
@implementation PywPayFootItemView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUp];
    }
    return self;
}
/**
 *  init
 */
-  (void)setUp
{
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 4;
    self.layer.masksToBounds = YES;
    
    [self addSubview:self.payBageImgview];
    [self addSubview:self.payIconImgView];
    [self addSubview:self.payNameLabel];
    
    WEAK_SELF;
    [self.payBageImgview mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [self.payIconImgView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.left.mas_equalTo(10);
        make.centerY.equalTo(strongSelf.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [self.payNameLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.left.equalTo(strongSelf.payIconImgView.mas_right).offset(5);
        make.right.equalTo(strongSelf).offset(-10);
        make.centerY.equalTo(strongSelf.mas_centerY);
        make.height.mas_equalTo(30);
    }];
}

#pragma mark - getter

- (UIImageView *)payBageImgview
{
    if (!_payBageImgview) {
        _payBageImgview  = [[UIImageView alloc] initWithImage:[UIImage imageWithName:@"img_choice60_pressed"]];
    }
    return _payBageImgview;
}
- (UIImageView *)payIconImgView
{
    if (!_payIconImgView) {
        _payIconImgView = [UIImageView new];
    }
    return _payIconImgView;
}

- (UILabel *)payNameLabel
{
    if (!_payNameLabel) {
        _payNameLabel = [UILabel new];
        _payNameLabel.font = [UIFont systemFontOfSize:14.f];
        _payNameLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _payNameLabel;
}
#pragma mark - setter

- (void)setHighlighted:(BOOL)highlighted
{
    [super setHighlighted:highlighted];
    
    [self setBorderColor:highlighted];
    
}

- (void)setSelected:(BOOL)selected
{
    [super setSelected:selected];
    
    [self setBorderColor:selected];
}
/**
 *  修改选中状态
 */
- (void)setBorderColor:(BOOL)selected
{
    if (self.selected || selected) {
        self.layer.borderColor = UIColorFromRGB(0xFFB22D).CGColor;
        self.layer.borderWidth = 2;
        self.payBageImgview.hidden = NO;
        self.payBageImgview.image = [UIImage imageWithName:@"img_choice60_pressed"];
    }else {
        self.layer.borderColor = UIColorFromRGB(0xCFCFCF).CGColor;
        self.layer.borderWidth = .5f;
        self.payBageImgview.hidden = YES;
        if (self.payWay == 3) {
            self.payBageImgview.hidden = NO;
            self.payBageImgview.image = [UIImage imageWithName:@"img_tuijian60_normal"];
        }
    }
}

@end

@interface PywPayFootView()
@property (nonatomic,strong) UILabel *promptLabel;
@property (nonatomic,strong) UIScrollView *mainScrollView;
/**
 *  支付方式
 */
@property (nonatomic,strong) NSArray *payWays;
/**
 *  支付方式按钮数组
 */
@property (nonatomic,strong) NSMutableArray *payWayItems;
/**
 *  点击支付方式回调
 */
@property (nonatomic,copy) SelectPayWays selectPayWays;
/**
 *  当前支付方式默认选择支付宝|3
 */
@property (nonatomic,assign) NSInteger currentPayWay;

@end


@implementation PywPayFootView
const CGFloat PayFootItemGap = 10.f;//支付方式按钮间隔
const CGFloat PayFootItemHeight = 44.f;//支付方式按钮高度

- (instancetype)initWithSelectPayWays:(SelectPayWays)selectPayWays
{
    _payWays = [self setUpCellContents];
    CGFloat height = 0;
    if (_payWays.count > 3) {
        height = 150;
    }else if (_payWays.count > 0) {
        height = 100;
    }
    CGRect frame = CGRectMake(0, 0, SCREEN_WIDTH, height);
    self = [super initWithFrame:frame];
    if (self) {
        self.selectPayWays = selectPayWays;
        [self setUp];
    }
    return self;
}
#pragma mark - init

- (void)setUp
{
    self.currentPayWay = PAY_WAY_TYPE_ALIPAY;
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.promptLabel];
    [self addSubview:self.mainScrollView];
    
    WEAK_SELF;
    [self.promptLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.left.top.mas_equalTo(10);
        make.width.mas_equalTo(SCREEN_WIDTH-20);
        make.height.mas_equalTo(20);
    }];
    
    [self.mainScrollView mas_makeConstraints:^(PywMASConstraintMaker *make) {
        STRONG_SELF(weakSelf);
        make.left.mas_equalTo(0);
        make.top.equalTo(strongSelf.promptLabel.mas_bottom).offset(10);
        make.width.mas_equalTo(SCREEN_WIDTH-10);
        make.height.mas_equalTo(PayFootItemHeight*2 + PayFootItemGap);
    }];
}

- (NSArray *)setUpCellContents
{
    NSMutableArray *contents = [[NSMutableArray alloc] init];
    NSArray *pay_type_order = [[PywAppInfo sharedAppInfo].pay_type_order componentsSeparatedByString:@","];
    for (NSInteger i = 0; i < pay_type_order.count; i ++) {
        NSString *payWay = pay_type_order[i];
        if ([self isShowPayWays:[payWay integerValue]]) {
            [contents addObject:payWay];
        }
    }
    return contents;
    
}

#pragma mark - Property
- (NSMutableArray *)payWayItems
{
    if (!_payWayItems) {
        _payWayItems = [[NSMutableArray alloc] init];
    }
    return _payWayItems;
}
- (UILabel *)promptLabel
{
    if (!_promptLabel) {
        _promptLabel = [UILabel new];
        _promptLabel.text = @"请选择支付方式";
        _promptLabel.textColor = UIColorFromRGB(0x999999);
        _promptLabel.font = [UIFont systemFontOfSize:14.f];
    }
    return _promptLabel;
}

- (UIScrollView *)mainScrollView
{
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] init];
    }
    return _mainScrollView;
}

#pragma mark - Actions
/**
 *  是否要显示支付方式
 */
- (BOOL)isShowPayWays:(NSInteger)payWay
{
    NSArray *payWays = [NSArray arrayWithObjects:
                        @(PAY_WAY_TYPE_WECHAT),
                        @(PAY_WAY_TYPE_ALIPAY),
                        @(PAY_WAY_TYPE_UNION),nil];
    BOOL isContain = NO;
    for (NSString *pay in payWays) {
        if ([pay integerValue] == payWay) {
            isContain = YES;
            break;
        }
    }
    if (!isContain || (payWay == PAY_WAY_TYPE_WECHAT && ![PywAppInfo sharedAppInfo].is_wxpay)) {
        return NO;
    }
    return YES;
    
}
/**
 *  刷新界面
 *  旋转屏幕也会刷新界面
 */
- (void)reloadFootViewPortrait:(BOOL)portrait
{
    [self.mainScrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.payWayItems removeAllObjects];
    NSInteger w;
    NSInteger h;
    for (NSInteger i = 0; i < self.payWays.count; i ++) {
        
        NSInteger payWay = [[self.payWays objectAtIndex:i] integerValue];
        if (portrait) {
            NSInteger itemCount = 3;
            w = i%itemCount;
            h = i/itemCount;
            CGFloat width = ((SCREEN_WIDTH - 20) - PayFootItemGap *(itemCount + 1))/itemCount;
            CGRect frame = CGRectMake(PayFootItemGap + (PayFootItemGap + width) * w,
                                      (PayFootItemHeight + PayFootItemGap) * h,
                                      width,
                                      PayFootItemHeight);
            PywPayFootItemView *itemView = [self itemView:frame payWay:payWay];
            [self.mainScrollView addSubview:itemView];
            [self.mainScrollView setContentSize:CGSizeMake(0, 0)];
            
            [self.payWayItems addObject:itemView];
            
        }else {
            CGFloat width = 106.f;
            CGRect frame = CGRectMake(PayFootItemGap + (PayFootItemGap + width) * i,0,width,PayFootItemHeight);
            PywPayFootItemView *itemView = [self itemView:frame payWay:payWay];
            [self.mainScrollView addSubview:itemView];
            
            CGFloat contentWidth = MAX(SCREEN_WIDTH, (PayFootItemGap + width) * self.payWays.count);
            [self.mainScrollView setContentSize:CGSizeMake(contentWidth, 0)];
            
            [self.payWayItems addObject:itemView];
        }
    }
    
    BOOL selected = NO;
    for (PywPayFootItemView *itemView in self.payWayItems) {
        if (itemView.selected) {
            selected = YES;
            break;
        }
    }
    if (!selected) {
        PywPayFootItemView *itemView = [self.payWayItems firstObject];
        itemView.selected = YES;
    }
}


- (PywPayFootItemView *)itemView:(CGRect)frame
                          payWay:(NSInteger)payWay
{
    PywPayFootItemView *itemView = [[PywPayFootItemView alloc] initWithFrame:frame];
    itemView.payWay = payWay;
    itemView.selected = (payWay == self.currentPayWay?YES:NO);
    NSDictionary *dictionary = [self payFootItemDictionary];
    NSDictionary *dict = [dictionary objectForKey:@(payWay)];
    if (dict) {
        itemView.payNameLabel.text = dict[@"title"];
        itemView.payIconImgView.image = [UIImage imageWithName:dict[@"icon"]];
        [itemView setAccessibilityIdentifier:[dict objectForKey:@"accessibilityId"]];
    }
    [itemView addTarget:self
                 action:@selector(itemViewSelected:)
       forControlEvents:UIControlEventTouchUpInside];
    return itemView;
}

/**
 *  点击支付方式
 */
- (void)itemViewSelected:(PywPayFootItemView *)sender
{
    for (PywPayFootItemView *itemView in self.payWayItems) {
        itemView.selected = NO;
    }
    sender.selected = YES;
    NSInteger payWay = sender.payWay;
    self.currentPayWay = payWay;
    if (self.selectPayWays) {
        self.selectPayWays(payWay);
    }
}
/*
 *  封装支付方式按钮的标题和icon
 */
- (NSDictionary *)payFootItemDictionary
{
    MutableDictionary *dictionary = [[MutableDictionary alloc] init];
    [dictionary setObject:@{@"title":@"微信支付",@"icon":@"icon_wechat_normal",@"accessibilityId":@"recharge_wechat"} forKey:@(PAY_WAY_TYPE_WECHAT)];
    [dictionary setObject:@{@"title":@"支付宝",@"icon":@"icon_alipay_normal",@"accessibilityId":@"recharge_alipay"} forKey:@(PAY_WAY_TYPE_ALIPAY)];
    [dictionary setObject:@{@"title":@"银  联",@"icon":@"icon_upcash_normal",@"accessibilityId":@"recharge_unionPay"} forKey:@(PAY_WAY_TYPE_UNION)];
    return dictionary;
}

@end
